//
//  XMChangeNameView.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/14.
//

#import "XMChangeNameView.h"

@interface XMChangeNameView ()

@property (nonatomic, strong) UILabel *wordNumLabel;

@end

@implementation XMChangeNameView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    self.backgroundColor = kColorHEX(0xF0F0F0);
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), 0, self.width - KScaleWidth(16.0)*2, KScaleWidth(31.0)) font:KFontSize(15.0) textColor:kColorHEX(0x666666)];
    titleLabel.text = @"7天内可修改一次名字";
    [self addSubview:titleLabel];
    
    UIView *nameBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0 + KScaleWidth(31.0), self.width, KScaleWidth(56.0))];
    nameBgView.backgroundColor = kColorHEX(0xffffff);
    [self addSubview:nameBgView];
    
    [nameBgView addSubview:self.nameTextField];
    [nameBgView addSubview:self.wordNumLabel];
    
    UILabel *explainLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), nameBgView.maxY, self.width - KScaleWidth(16.0)*2, KScaleWidth(37.0)) font:KFontSize(15.0) textColor:kColorHEX(0x666666)];
    explainLabel.text = @"请设置2-24个字符，不包括@<>/等无效字符";
    [self addSubview:explainLabel];
}

- (void)inputChanged:(UITextField *)textField {
    if (_nameTextField.text.length > 24) {
        _nameTextField.text = [_nameTextField.text substringToIndex:24];
    }
    _wordNumLabel.text = [NSString stringWithFormat:@"%ld/24", _nameTextField.text.length];
}

- (void)setContentWithName:(NSString *)nameStr {
    _nameTextField.text = nameStr;
    _wordNumLabel.text = [NSString stringWithFormat:@"%ld/24", nameStr.length];
}

#pragma mark - Init
- (UITextField *)nameTextField {
    if (!_nameTextField) {
        _nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), 0, self.width - KScaleWidth(16.0)*2, KScaleWidth(56.0))];
        _nameTextField.font = KFontSize(17.0);
        [_nameTextField addTarget:self action:@selector(inputChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _nameTextField;
}

- (UILabel *)wordNumLabel {
    if (!_wordNumLabel) {
        _wordNumLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.width - KScaleWidth(15.0) - KScaleWidth(50.0), 0, KScaleWidth(50.0), KScaleWidth(56.0)) font:KFontSize(15.0) textColor:kColorHEX(0x666666) textAlignment:NSTextAlignmentRight];
    }
    return _wordNumLabel;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
